| python |
|---|
| print("Hello, World!") # Output: Hello, World! |
| python |
|---|
| x = 10 name = "Alice" is_active = True |
| python |
|---|
| int("5") # Converts string to integer float(10) # Converts integer to float |
| python |
|---|
| if age >= 18: print("Adult") elif age >= 13: print("Teen") else: print("Child") |
| python |
|---|
| for i in range(5): print(i) |
| python |
|---|
| count = 0 while count < 5: print(count) count += 1 |
| python |
|---|
|
greet(name): return f"Hello, {name}!" print(greet("Alice")) |
| python |
|---|
| def greet(name="World"): return f"Hello, {name}!" |
| python |
|---|
| fruits = ["apple", "banana", "cherry"] fruits.append("date") print(fruits) |
| python |
|---|
|
person = {"name": "Alice", "age": 25} print(person["name"]) |
| python |
|---|
|
nums = {1, 2, 3} nums.add(4) |
| python |
|---|
| colors = ("red", "green", "blue") |
| python |
|---|
|
class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): return f"Hi, I'm {self.name}." person = Person("Alice", 25) print(person.greet()) |
| python |
|---|
|
with open("file.txt", "r") as file: content = file.read() |
| python |
|---|
|
with open("file.txt", "w") as file: file.write("Hello, World!") |
| python |
|---|
|
try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!") |
| python |
|---|
| import math print(math.sqrt(16)) |
| python |
|---|
| squares = [x**2 for x in range(10)] |
| python |
|---|
| add = lambda x, y: x + y print(add(3, 5)) |
| python |
|---|
| import requests response = requests.get ("https://api.example.com/data") print(response.json()) |